﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace GiMess
{
    public partial class MainForm : Form
    {
        #region Login Controls

        private Panel   loginPanel;
        private PictureBox logoBox;
        private TextBox userNameTextBox;
        private TextBox userPasswordTextBox;
        private Label   userNameLabel;
        private Label   userPasswordLabel;
        private Button  signInButton;

        #endregion

        public GiNetClient  giClient;

        public MainForm()
        {
            InitializeComponent();
            InitLoginScreen();
            giClient = new GiNetClient();
        }

        private void InitLoginScreen()
        {
            Size comSize = new Size(160, 20);
            loginPanel = new Panel();
            
            //loginPanel.Dock = DockStyle.Fill;
            loginPanel.Size     = new Size(this.Width, this.Height);
            loginPanel.Location = new Point(0, 0);
            loginPanel.Anchor   = (byte)AnchorStyles.Left + (byte)AnchorStyles.Right + (byte)AnchorStyles.Top + AnchorStyles.Bottom;
            loginPanel.Name     = "loginPanel";

            logoBox = new PictureBox();
            logoBox.Size = new Size(90, 100);
            logoBox.Image = GiMess.Properties.Resources.logo as Image;
            logoBox.Location = new Point(90, 15);
            logoBox.SizeMode = PictureBoxSizeMode.Zoom;

            userNameTextBox = new TextBox();
            userNameTextBox.Size     = comSize;
            userNameTextBox.Location = new Point(50, 150);
            userNameTextBox.Name     = "userNameTextBox";

            userPasswordTextBox = new TextBox();
            userPasswordTextBox.Size     = comSize;
            userPasswordTextBox.Location = new Point(50, 200);
            userPasswordTextBox.Name     = "userPasswordTextBox";
            userPasswordTextBox.PasswordChar = '●';

            userNameLabel = new Label();
            userNameLabel.Size      = comSize;
            userNameLabel.Location  = new Point(50, 130);
            userNameLabel.Text      = "Username:";
            userNameLabel.Name      = "userNameLabel";

            userPasswordLabel = new Label();
            userPasswordLabel.Size     = comSize;
            userPasswordLabel.Location = new Point(50, 180);
            userPasswordLabel.Text     = "Password:";
            userPasswordLabel.Name     = "userPasswordLabel";


            signInButton = new Button();
            signInButton.Size      = new Size(70, 27);
            signInButton.Location  = new Point(95, 230);
            signInButton.Text      = "Sign In";
            signInButton.Name      = "signInButton";
            signInButton.Click    += new EventHandler(signInButton_Click);

            loginPanel.Controls.Add(logoBox);
            loginPanel.Controls.Add(userNameLabel);
            loginPanel.Controls.Add(userPasswordLabel);
            loginPanel.Controls.Add(userNameTextBox);
            loginPanel.Controls.Add(userPasswordTextBox);
            loginPanel.Controls.Add(signInButton);

            this.Controls.Add(loginPanel);
            this.Controls["loginPanel"].BringToFront();
        }

        public void HideLoginScreen()
        {
            loginPanel.Invoke(new MethodInvoker(delegate() { loginPanel.Visible = false; }));
        }

        //deprecated
        public void ListInvokeAddListUser(string firstName, string lastName, string username, string group)
        {
            friendsView.Invoke(new MethodInvoker(delegate() { ListAddListUser(firstName, lastName, username, group); }));
        }

        public void ListInvokeAddListUser(UserFriend friend)
        {
            friendsView.Invoke(new MethodInvoker(delegate() { ListAddListUser(friend); }));
        }

        public void ListInvokeSetUserOnline(string username, bool online)
        {
            friendsView.Invoke(new MethodInvoker(delegate() { ListSetUserOnline(username, online); }));
        }

        private void ListSetUserOnline(string username, bool online)
        {
            for (int i = 0; i < friendsView.Nodes.Count; i++)
            {
                if (friendsView.Nodes[i].Nodes[username] == null) continue;
                if (online)
                    friendsView.Nodes[i].Nodes[username].NodeFont = new Font(DefaultFont, FontStyle.Bold);
                else
                    friendsView.Nodes[i].Nodes[username].NodeFont = new Font(DefaultFont, FontStyle.Regular);
            }
        }
            

        private void ListAddListUser(UserFriend friend)
        {
            TreeNode groupNode;
            TreeNode newNode;
            groupNode = ListGetGroup(friend.Group);
            if (groupNode == null)
            {
                groupNode = ListCreateGroup(friend.Group);
                groupNode.Tag = null; //in tag pun null daca e grup
            }

            if (friend.Status != "") 
                newNode = groupNode.Nodes.Insert(groupNode.Nodes.Count, friend.FirstName + " " + friend.LastName + " - " + friend.Status);
            else
                newNode = groupNode.Nodes.Insert(groupNode.Nodes.Count, friend.FirstName + " " + friend.LastName);
            newNode.Name = friend.Username; //nefolositor???
            newNode.Tag = friend;
            if (friend.Online)
                newNode.NodeFont = new Font(DefaultFont, FontStyle.Bold);
            friendsView.Sort();
            friendsView.ExpandAll();

        }
        
        //deprecated
        private void ListAddListUser(string firstName, string lastName, string username, string group)
        {
            TreeNode groupNode;
            TreeNode newNode;
            groupNode = ListGetGroup(group);
            if (groupNode == null)
            {
                groupNode = ListCreateGroup(group);
                groupNode.Tag = false;
            }

            newNode = groupNode.Nodes.Insert(groupNode.Nodes.Count, firstName + " " + lastName);
            newNode.Name = username; //ar trebuii pus username in node.Tag??? corect: da...
            newNode.Tag = true; // in tag trec adevarat daca nodu e user si fals daca e grup
        }

        private TreeNode ListCreateGroup(string group)
        {
            return friendsView.Nodes.Insert(friendsView.Nodes.Count, group);
        }

        private TreeNode ListGetGroup(string group)
        {
            TreeNode node;

            for(int i = 0; i<friendsView.Nodes.Count; i++)
            {
                node = friendsView.Nodes[i];
                if (node.Text == group) return node;
            }
            return null;
        }

        private void friendsView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if(e.Node.Tag != null)
                FormsManager.OpenIMForm(e.Node.Text, e.Node.Name);
        }

        private void signInButton_Click(object sender, EventArgs e)
        {
            AuthInfo.UserName = userNameTextBox.Text;
            giClient.Login(userNameTextBox.Text, userPasswordTextBox.Text);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            giClient.Disconnect();
            Application.Exit();
        }

        private void signOutToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
